/**
 * $Revision: 1.4 $
 * $Date: 2010/07/19 11:23:21 $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware;


import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.component.TitlePanel;
import org.jivesoftware.spark.component.WrappedLabel;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

import javax.security.auth.Subject;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JButton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.security.Principal;
import java.util.Properties;
import java.io.File;

/**
 * Allows users to configure startup options.
 *
 * @author Derek DeMoro
 * @author Jay Kline
 */
public class LoginSettingDialog implements PropertyChangeListener {

    private LocalPreferences localPreferences;


    private JDialog optionsDialog;
    private JOptionPane optionPane;

    private GeneralPanel generalPanel;
    private ProxyPanel proxyPanel;
    private PkiPanel pkiPanel;
    private SsoPanel ssoPanel;

    
    private DoubleChannelPanel channelPanel;

    /**
     * Empty Constructor.
     */
    public LoginSettingDialog() {
        localPreferences = SettingsManager.getLocalPreferences();
        generalPanel = new GeneralPanel();
        proxyPanel = new ProxyPanel();
        ssoPanel = new SsoPanel();
        pkiPanel = new PkiPanel();
        
        channelPanel = new DoubleChannelPanel();
    }

    /**
     * Invokes the OptionsDialog.
     *
     * @param owner the parent owner of this dialog. This is used for correct parenting.
     * @return true if the options have been changed.
     */
    public boolean invoke(JFrame owner) {
        JTabbedPane tabbedPane = new JTabbedPane();
        TitlePanel titlePanel;

        // Create the title panel for this dialog
        titlePanel = new TitlePanel(Res.getString("title.advanced.connection.preferences"), "", SparkRes.getImageIcon(SparkRes.BLANK_24x24), true);



        tabbedPane.addTab(Res.getString("tab.general"), generalPanel);
        tabbedPane.addTab(Res.getString("tab.proxy"), proxyPanel);
//        tabbedPane.addTab("SSO", ssoPanel);
//        tabbedPane.addTab("PKI", pkiPanel);

       // tabbedPane.addTab("双频道", channelPanel);
        
        // Construct main panel w/ layout.
        final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // The user should only be able to close this dialog.
        Object[] options = {Res.getString("ok"), Res.getString("cancel"), Res.getString("use.default")};
        optionPane = new JOptionPane(tabbedPane, JOptionPane.PLAIN_MESSAGE,
                JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

        mainPanel.add(optionPane, BorderLayout.CENTER);

        optionsDialog = new JDialog(owner, Res.getString("title.preferences"), true);
        optionsDialog.setContentPane(mainPanel);
        optionsDialog.pack();


        optionsDialog.setLocationRelativeTo(owner);
        optionPane.addPropertyChangeListener(this);

        optionsDialog.setResizable(true);
        optionsDialog.setVisible(true);
        optionsDialog.toFront();
        optionsDialog.requestFocus();

        return true;
    }

    /**
     * PropertyChangeEvent is called when the user either clicks the Cancel or
     * OK button.
     *
     * @param e the property change event.
     */
    public void propertyChange(PropertyChangeEvent e) {
        String value = (String)optionPane.getValue();
        if (Res.getString("cancel").equals(value)) {
            optionsDialog.setVisible(false);
        }
        else if (Res.getString("ok").equals(value)) {

            boolean valid = generalPanel.validate_settings();
            valid = valid && proxyPanel.validate_settings();
            valid = valid && ssoPanel.validate_settings();
            valid = valid && pkiPanel.validate_settings();

            valid = valid && channelPanel.validate_settings();

            if(valid) {
                generalPanel.saveSettings();
                proxyPanel.saveSettings();
                ssoPanel.saveSettings();
                pkiPanel.saveSettings();
                
                channelPanel.saveSettings();
                
                SettingsManager.saveSettings();
                optionsDialog.setVisible(false);
            } else {
                optionPane.removePropertyChangeListener(this);
                optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                optionPane.addPropertyChangeListener(this);
            }
        }
        else {
            // Some unknown operation happened
            optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
        }
    }

    /**
     * Internal class to set General settings
     */
    private class GeneralPanel extends JPanel implements ActionListener {

        private JCheckBox autoDiscoverBox = new JCheckBox();
        private JLabel portLabel = new JLabel();
        private JTextField portField = new JTextField();
        private JLabel xmppHostLabel = new JLabel();
        private JTextField xmppHostField = new JTextField();
        private JLabel timeOutLabel = new JLabel();
        private JTextField timeOutField = new JTextField();
        private JLabel resourceLabel = new JLabel();
        private JTextField resourceField = new JTextField();
        private JCheckBox autoLoginBox = new JCheckBox();
        private JCheckBox useSSLBox = new JCheckBox();
        private JCheckBox compressionBox = new JCheckBox(); 
        private JCheckBox debuggerBox = new JCheckBox();
        private JCheckBox uiaCheckBox = new JCheckBox();



        public GeneralPanel() {
            ResourceUtils.resLabel(portLabel, portField, Res.getString("label.port"));
            ResourceUtils.resLabel(timeOutLabel, timeOutField, Res.getString("label.response.timeout"));
            ResourceUtils.resButton(autoLoginBox, Res.getString("label.auto.login"));
            ResourceUtils.resButton(useSSLBox, Res.getString("label.old.ssl"));
            ResourceUtils.resLabel(xmppHostLabel, xmppHostField, Res.getString("label.host"));
            ResourceUtils.resButton(autoDiscoverBox, Res.getString("checkbox.auto.discover.port"));
            ResourceUtils.resLabel(resourceLabel, resourceField, Res.getString("label.resource"));
            ResourceUtils.resButton(compressionBox, Res.getString("checkbox.use.compression"));
            ResourceUtils.resButton(debuggerBox, Res.getString("checkbox.start.debugger.on.startup"));
            ResourceUtils.resButton(uiaCheckBox,"启动统一身份认证");
            
            portField.setText(Integer.toString(localPreferences.getXmppPort()));
            timeOutField.setText(Integer.toString(localPreferences.getTimeOut()));
            autoLoginBox.setSelected(localPreferences.isAutoLogin());
            useSSLBox.setSelected(localPreferences.isSSL());
            xmppHostField.setText(localPreferences.getXmppHost());
            resourceField.setText(localPreferences.getResource());
            uiaCheckBox.setSelected(localPreferences.isUiaEnabled());

            if (localPreferences.getResource() == null) {
                resourceField.setText("netcall");
            }


            autoDiscoverBox.addActionListener(this);

            autoDiscoverBox.setSelected(!localPreferences.isHostAndPortConfigured());
            updateAutoDiscovery();

            compressionBox.setSelected(localPreferences.isCompressionEnabled());


            debuggerBox.setSelected(localPreferences.isDebuggerEnabled());

            final JPanel connectionPanel = new JPanel();
            connectionPanel.setLayout(new GridBagLayout());
            connectionPanel.setBorder(BorderFactory.createTitledBorder(Res.getString("group.connection")));

            setLayout(new GridBagLayout());
            add(autoDiscoverBox, 
                        new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            connectionPanel.add(xmppHostLabel, 
                        new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            connectionPanel.add(xmppHostField, 
                        new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 200, 0));
            connectionPanel.add(portLabel, 
                        new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            connectionPanel.add(portField, 
                        new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 50, 0));
            add(connectionPanel, 
                        new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.BOTH, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(resourceLabel, 
                        new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(resourceField, 
                        new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 100, 0));
            add(timeOutLabel, 
                        new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(timeOutField, 
                        new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 50, 0));
            add(useSSLBox, 
                        new GridBagConstraints(0, 4, 2, 1, 0.0, 1.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(compressionBox, 
                        new GridBagConstraints(0, 5, 2, 1, 0.0, 1.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
//            add(debuggerBox, 
//                        new GridBagConstraints(0, 6, 2, 1, 0.0, 1.0, 
//                                                GridBagConstraints.WEST, 
//                                                GridBagConstraints.HORIZONTAL, 
//                                                new Insets(5, 5, 5, 5), 0, 0));
        
//            add(uiaCheckBox, 
//                    new GridBagConstraints(0, 7, 2, 1, 0.0, 1.0, 
//                                          GridBagConstraints.WEST, 
//                                          GridBagConstraints.HORIZONTAL, 
//                                          new Insets(5, 5, 5, 5), 0, 0));
        }

        /**
        * Updates local preferences with auto discovery settings.
        */
        private void updateAutoDiscovery() {
            boolean isSelected = autoDiscoverBox.isSelected();
            xmppHostField.setEnabled(!isSelected);
            portField.setEnabled(!isSelected);
            localPreferences.setHostAndPortConfigured(!isSelected);
            SettingsManager.saveSettings();
        }


        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == autoDiscoverBox) {
                updateAutoDiscovery();
            }
        }

        public boolean validate_settings() {
            String timeOut = timeOutField.getText();
            String port = portField.getText();
            String resource = resourceField.getText();

            boolean valid = true;

            try {
                Integer.valueOf(timeOut);
            }
            catch (NumberFormatException numberFormatException) {
                JOptionPane.showMessageDialog(optionsDialog, Res.getString("message.supply.valid.timeout"),
                        Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                timeOutField.requestFocus();
                valid = false;
            }

            try {
                Integer.valueOf(port);
            }
            catch (NumberFormatException numberFormatException) {
                JOptionPane.showMessageDialog(optionsDialog, Res.getString("message.supply.valid.port"),
                        Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                portField.requestFocus();
                valid = false;
            }

            if (!ModelUtil.hasLength(resource)) {
                JOptionPane.showMessageDialog(optionsDialog, Res.getString("message.supply.resource"),
                        Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                resourceField.requestFocus();
                valid = false;
            }

            return valid;
        }

        public void saveSettings() {

            localPreferences.setTimeOut(Integer.parseInt(timeOutField.getText()));
            localPreferences.setXmppPort(Integer.parseInt(portField.getText()));
            localPreferences.setSSL(useSSLBox.isSelected());
            localPreferences.setXmppHost(xmppHostField.getText());
            localPreferences.setCompressionEnabled(compressionBox.isSelected());
            localPreferences.setDebuggerEnabled(debuggerBox.isSelected());
            localPreferences.setResource(resourceField.getText());
            localPreferences.setUiaEnabled(uiaCheckBox.isSelected());
            SettingsManager.saveSettings();
        }
    }


    /**
     * Internal class to allow setting of proxies within Spark.
     */
    private class ProxyPanel extends JPanel {
        private JCheckBox useProxyBox = new JCheckBox();
        private JComboBox protocolBox = new JComboBox();
        private JTextField hostField = new JTextField();
        private JTextField portField = new JTextField();
        private JTextField usernameField = new JTextField();
        private JPasswordField passwordField = new JPasswordField();

        /**
         * Construct UI.
         */
        public ProxyPanel() {
            JLabel protocolLabel = new JLabel();
            JLabel hostLabel = new JLabel();
            JLabel portLabel = new JLabel();
            JLabel usernameLabel = new JLabel();
            JLabel passwordLabel = new JLabel();

            protocolBox.addItem("SOCKS");
            protocolBox.addItem("HTTP");

            // Add ResourceUtils
            ResourceUtils.resButton(useProxyBox, Res.getString("checkbox.use.proxy.server"));
            ResourceUtils.resLabel(protocolLabel, protocolBox, Res.getString("label.protocol"));
            ResourceUtils.resLabel(hostLabel, hostField, Res.getString("label.host"));
            ResourceUtils.resLabel(portLabel, portField, Res.getString("label.port"));
            ResourceUtils.resLabel(usernameLabel, usernameField, Res.getString("label.username"));
            ResourceUtils.resLabel(passwordLabel, passwordField, Res.getString("label.password"));

            setLayout(new GridBagLayout());
            add(useProxyBox, 
                        new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(protocolLabel, 
                        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(protocolBox, 
                        new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(hostLabel, 
                        new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(hostField, 
                        new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(portLabel, 
                        new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(portField, 
                        new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(usernameLabel, 
                        new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(usernameField, 
                        new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(passwordLabel, 
                        new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(passwordField, 
                        new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, 
                                                GridBagConstraints.WEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));


            useProxyBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    enableFields(useProxyBox.isSelected());
                }
            });

            // Check localSettings
            if (localPreferences.isProxyEnabled()) {
                useProxyBox.setSelected(true);
            }

            enableFields(useProxyBox.isSelected());

            if (ModelUtil.hasLength(localPreferences.getHost())) {
                hostField.setText(localPreferences.getHost());
            }

            if (ModelUtil.hasLength(localPreferences.getPort())) {
                portField.setText(localPreferences.getPort());
            }

            if (ModelUtil.hasLength(localPreferences.getProxyPassword())) {
                passwordField.setText(localPreferences.getProxyPassword());
            }

            if (ModelUtil.hasLength(localPreferences.getProxyUsername())) {
                usernameField.setText(localPreferences.getProxyUsername());
            }

            if (ModelUtil.hasLength(localPreferences.getProtocol())) {
                protocolBox.setSelectedItem(localPreferences.getProtocol());
            }

        }

        /**
         * Enables the fields of the proxy panel.
         *
         * @param enable true if all fields should be enabled, otherwise false.
         */
        private void enableFields(boolean enable) {
            Component[] comps = getComponents();
            for (Component comp1 : comps) {
                if (comp1 instanceof JTextField || comp1 instanceof JComboBox) {
                    JComponent comp = (JComponent) comp1;
                    comp.setEnabled(enable);
                }
            }
        }

        /**
         * Returns true if a proxy is set.
         *
         * @return true if a proxy is set.
         */
        public boolean useProxy() {
            return useProxyBox.isSelected();
        }

        /**
         * Returns the protocol to use for this proxy.
         *
         * @return the protocol.
         */
        public String getProtocol() {
            return (String)protocolBox.getSelectedItem();
        }

        /**
         * Returns the host to use for this proxy.
         *
         * @return the host.
         */
        public String getHost() {
            return hostField.getText();
        }

        /**
         * Returns the port to use with this proxy.
         *
         * @return the port to use.
         */
        public String getPort() {
            return portField.getText();
        }

        /**
         * Returns the username to use with this proxy.
         *
         * @return the username.
         */
        public String getUsername() {
            return usernameField.getText();
        }

        /**
         * Returns the password to use with this proxy.
         *
         * @return the password.
         */
        public String getPassword() {
            return new String(passwordField.getPassword());
        }

        public boolean validate_settings() {
            boolean valid = true;

            if(useProxyBox.isSelected()) {
                try {
                    Integer.valueOf(portField.getText());
                }
                catch (NumberFormatException numberFormatException) {
                    JOptionPane.showMessageDialog(optionsDialog, Res.getString("message.supply.valid.port"),
                            Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                    portField.requestFocus();
                    valid = false;
                }
    
                if (!ModelUtil.hasLength(hostField.getText())) {
                    JOptionPane.showMessageDialog(optionsDialog, Res.getString("message.supply.valid.host"),
                            Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                    hostField.requestFocus();
                    valid = false;
                }
            }
            return valid;
        }

        /**
         * Persist the proxy settings to local preferences.
         */
        public void saveSettings() {
            localPreferences.setProxyEnabled(useProxyBox.isSelected());
            if (ModelUtil.hasLength(getProtocol())) {
                localPreferences.setProtocol(getProtocol());
            }

            if (ModelUtil.hasLength(getHost())) {
                localPreferences.setHost(getHost());
            }

            if (ModelUtil.hasLength(getPort())) {
                localPreferences.setPort(getPort());
            }

            if (ModelUtil.hasLength(getUsername())) {
                localPreferences.setProxyUsername(getUsername());
            }

            if (ModelUtil.hasLength(getPassword())) {
                localPreferences.setProxyPassword(getPassword());
            }

            if (!localPreferences.isProxyEnabled()) {
                Properties props = System.getProperties();
                props.remove("socksProxyHost");
                props.remove("socksProxyPort");
                props.remove("http.proxyHost");
                props.remove("http.proxyPort");
                props.remove("http.proxySet");
            }
            else {
                String host = localPreferences.getHost();
                String port = localPreferences.getPort();
                String protocol = localPreferences.getProtocol();

                boolean isValid = ModelUtil.hasLength(host) && ModelUtil.hasLength(port);

                if (isValid) {
                    if (protocol.equals("SOCKS")) {
                        System.setProperty("socksProxyHost", host);
                        System.setProperty("socksProxyPort", port);
                    }
                    else {
                        System.setProperty("http.proxySet", "true");

                        // Set https settings
                        System.setProperty("https.proxyHost", host);
                        System.setProperty("https.proxyPort", port);

                        // Set http settings
                        System.setProperty("http.proxyHost", host);
                        System.setProperty("http.proxyPort", port);
                    }
                }
                else {
                    localPreferences.setProxyEnabled(false);
                }
            }
            SettingsManager.saveSettings();
        }
    }


    /**
     * Internal class to set SSO settings
     */
    private class SsoPanel extends JPanel implements ActionListener {
        private JCheckBox useSSOBox = new JCheckBox();
        private JPanel settingsPanel = new JPanel();
        private JCheckBox showAdvBox = new JCheckBox();
        private JLabel ssoRealmLabel = new JLabel();
        private JTextField ssoRealmField = new JTextField();
        private JLabel ssoKDCLabel = new JLabel();
        private JTextField ssoKDCField = new JTextField();
        private JLabel ssoMethodFileLabel = new JLabel();
        private JRadioButton ssoMethodFileRadio = new JRadioButton();
        private JLabel ssoMethodDNSLabel = new JLabel();
        private JRadioButton ssoMethodDNSRadio = new JRadioButton();
        private JLabel ssoMethodManualLabel = new JLabel();
        private JRadioButton ssoMethodManualRadio = new JRadioButton();
        private ButtonGroup ssoMethodRadio = new ButtonGroup();



        public SsoPanel() {
            ResourceUtils.resButton(useSSOBox, "&Use Single Sign-On (SSO) via GSSAPI");
            ResourceUtils.resButton(showAdvBox, Res.getString("title.advanced.connection.preferences"));

            useSSOBox.addActionListener(this);
            showAdvBox.addActionListener(this);

            final WrappedLabel wrappedLabel = new WrappedLabel();
            String principalName = null;
            try {
                principalName = getPrincipalName();
            }
            catch (Exception e) {
                // Ignore
            }

            if (ModelUtil.hasLength(principalName)) {
                wrappedLabel.setText("This will use the Desktop Account for \"" + principalName + "\" to login to the server.");
            }
            else {
                wrappedLabel.setText("Spark is unable to find the principal to use for Single Sign-On. This will prevent SSO from working.");
            }
            wrappedLabel.setBackground(Color.white);


            String method = localPreferences.getSSOMethod();
            if (ModelUtil.hasLength(method)) {
                if(method.equals("file")) {
                    ssoMethodFileRadio.setSelected(true);
                } else if(method.equals("dns")) {
                    ssoMethodDNSRadio.setSelected(true);
                } else if(method.equals("manual")) {
                    ssoMethodManualRadio.setSelected(true);
                }
                else {
                    ssoMethodFileRadio.setSelected(true);
                }
            }
            else {
                ssoMethodFileRadio.setSelected(true);
            }

            if (ModelUtil.hasLength(localPreferences.getSSORealm())) {
                ssoRealmField.setText(localPreferences.getSSORealm());
            }
            if (ModelUtil.hasLength(localPreferences.getSSOKDC())) {
                ssoKDCField.setText(localPreferences.getSSOKDC());
            }

            ssoMethodFileLabel.setText("Use krb5.conf or krb5.ini:");
            ssoMethodDNSLabel.setText("Use DNS:");
            ssoMethodManualLabel.setText("Specify Below:");
            ssoRealmLabel.setText("    Realm:");
            ssoKDCLabel.setText("    KDC:");

            ssoMethodRadio.add(ssoMethodFileRadio);
            ssoMethodRadio.add(ssoMethodDNSRadio);
            ssoMethodRadio.add(ssoMethodManualRadio);

            useSSOBox.setSelected(localPreferences.isSSOEnabled());

            ssoMethodFileRadio.setEnabled(localPreferences.isSSOEnabled());
            ssoMethodDNSRadio.setEnabled(localPreferences.isSSOEnabled());
            ssoMethodManualRadio.setEnabled(localPreferences.isSSOEnabled());
            ssoRealmField.setEnabled(localPreferences.isSSOEnabled());
            ssoKDCField.setEnabled(localPreferences.isSSOEnabled());

            showAdvBox.setSelected(localPreferences.getSSOAdv());
            settingsPanel.setVisible(localPreferences.getSSOAdv());

            setLayout(new GridBagLayout());
            add(useSSOBox, 
                        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(showAdvBox, 
                        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(wrappedLabel, 
                        new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, 
                                                GridBagConstraints.NORTHWEST,
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.setLayout(new GridBagLayout());
            settingsPanel.add(ssoMethodFileLabel, 
                        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoMethodFileRadio, 
                        new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoMethodDNSLabel, 
                        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoMethodDNSRadio, 
                        new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoMethodManualLabel, 
                        new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoMethodManualRadio, 
                        new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoRealmLabel, 
                        new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoRealmField, 
                        new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoKDCLabel, 
                        new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            settingsPanel.add(ssoKDCField, 
                        new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(settingsPanel, 
                        new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL,
                                                new Insets(5, 5, 5, 5), 100, 0));


        }


        /**
        * Returns the principal name if one exists.
        *
        * @return the name (ex. derek) of the principal.
        * @throws Exception thrown if a Principal was not found.
        */
        private String getPrincipalName() throws Exception {
            if(localPreferences.getDebug()) {
                System.setProperty("java.security.krb5.debug", "true");
            }
            System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
            GSSAPIConfiguration config = new GSSAPIConfiguration(false);
            Configuration.setConfiguration(config);

            LoginContext lc;
            try {
                lc = new LoginContext("com.sun.security.jgss.krb5.initiate");
                lc.login();
            }
            catch (LoginException le) {
                Log.debug(le.getMessage());
                return null;
            }

            Subject mySubject = lc.getSubject();

            for (Principal p : mySubject.getPrincipals()) {
                String name = p.getName();
                int indexOne = name.indexOf("@");
                if (indexOne != -1) {
                    return name;
                }
            }
            return null;
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == useSSOBox) {
                ssoMethodFileRadio.setEnabled(useSSOBox.isSelected());
                ssoMethodDNSRadio.setEnabled(useSSOBox.isSelected());
                ssoMethodManualRadio.setEnabled(useSSOBox.isSelected());
                ssoRealmField.setEnabled(useSSOBox.isSelected());
                ssoKDCField.setEnabled(useSSOBox.isSelected());
            }
            else if(e.getSource() == showAdvBox) {
                settingsPanel.setVisible(showAdvBox.isSelected());
            }
        }

        public boolean validate_settings() {

            boolean valid = true;

            if(useSSOBox.isSelected() && showAdvBox.isSelected()) {
                if(ssoMethodManualRadio.isSelected()) {
                    if(!ModelUtil.hasLength(ssoRealmField.getText())) {
                        JOptionPane.showMessageDialog(optionsDialog, "You must specify a realm",
                            Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                        ssoRealmField.requestFocus();
                        valid = false;
                    }
                    if(!ModelUtil.hasLength(ssoKDCField.getText())) {
                        JOptionPane.showMessageDialog(optionsDialog, "You must specify a KDC",
                            Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                        ssoKDCField.requestFocus();
                        valid = false;
                    }
                }
            }

            return valid;
        }

        public void saveSettings() {
            localPreferences.setSSOEnabled(useSSOBox.isSelected());
            if(ssoMethodFileRadio.isSelected()) {
                localPreferences.setSSOMethod("file");
            }
            else if(ssoMethodDNSRadio.isSelected()) {
                localPreferences.setSSOMethod("dns");
            }
            else if(ssoMethodManualRadio.isSelected()) {
                localPreferences.setSSOMethod("manual");
                localPreferences.setSSORealm(ssoRealmField.getText());
                localPreferences.setSSOKDC(ssoKDCField.getText());
            }
            else {
                localPreferences.setSSOMethod("file");
            }
            SettingsManager.saveSettings();
        }
    }


    /**
     * Internal class to set PKI settings
     */

    private class PkiPanel extends JPanel implements ActionListener {
        private JLabel       usePKILabel = new JLabel();
        private JCheckBox    usePKIBox = new JCheckBox();
        private JLabel       pkiStoreLabel = new JLabel();
        private JComboBox    pkiStore = new JComboBox();
        private JFileChooser fileChooser = new JFileChooser();
        private JButton      fileButton = new JButton();
        private JTextField   fileField = new JTextField();
        private JPanel       filePanel = new JPanel();
        private JLabel       trustStorePasswordLabel = new JLabel();
        private JPasswordField trustStorePassword = new JPasswordField();
        private JTextField   trustStoreField = new JTextField();
        private JButton      trustStoreButton = new JButton();
        private JPanel       trustStorePanel = new JPanel();

        public PkiPanel() {
            ResourceUtils.resButton(usePKIBox, "&Use PKI Authentication");
            ResourceUtils.resLabel(pkiStoreLabel, pkiStore, "Which PKI Method?");
            ResourceUtils.resButton(fileButton, "Choose File");
            ResourceUtils.resButton(trustStoreButton, "Choose File");
            ResourceUtils.resLabel(trustStorePasswordLabel, trustStorePassword, "Trust Store Password");


            pkiStore.addItem("Java Keystore");
            pkiStore.addItem("PKCS#11");
            //pkiStore.addItem("X.509 PEM File");
            pkiStore.addItem("Apple KeyChain");

            usePKIBox.setSelected(localPreferences.isPKIEnabled());

            if(ModelUtil.hasLength(localPreferences.getPKIStore())) {
                if(localPreferences.getPKIStore().equals("PKCS11")) {
                    pkiStore.setSelectedItem("PKCS#11");
                    if(ModelUtil.hasLength(localPreferences.getPKCS11Library())) {
                        fileField.setText(localPreferences.getPKCS11Library());
                    }
                    else {
                        fileField.setText("");
                    }
                }
                else if(localPreferences.getPKIStore().equals("X509")) {
                    pkiStore.setSelectedItem("X.509 PEM File");
                    //if(ModelUtil.hasLength(localPreferences.getPEMFile())) {
                    //    fileField.setText(localPreferences.getPEMFile());
                    //}
                    //else {
                        fileField.setText("");
                    //}
                }
                else if(localPreferences.getPKIStore().equals("Apple KeyChain")) {
                        fileField.setText("");
                }
                else {
                    pkiStore.setSelectedItem("Java Keystore");
                    if(ModelUtil.hasLength(localPreferences.getJKSPath())) {
                        fileField.setText(localPreferences.getJKSPath());
                    }
                    else {
                        fileField.setText("");
                    }
                }
            }
            else {
                pkiStore.setSelectedItem("Java Keystore");
                if(ModelUtil.hasLength(localPreferences.getJKSPath())) {
                    fileField.setText(localPreferences.getJKSPath());
                }
                else {
                    fileField.setText("");
                }
            }

            if(ModelUtil.hasLength(localPreferences.getTrustStorePath())) {
                trustStoreField.setText(localPreferences.getTrustStorePath());
            }

            if(ModelUtil.hasLength(localPreferences.getTrustStorePassword())) {
                trustStorePassword.setText(localPreferences.getTrustStorePassword());
            }

            pkiStore.setEnabled(usePKIBox.isSelected());
            filePanel.setEnabled(usePKIBox.isSelected());
            fileField.setEnabled(usePKIBox.isSelected());
            fileButton.setEnabled(usePKIBox.isSelected());


            setLayout(new GridBagLayout());

            add(usePKIBox, 
                        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(usePKILabel,
                        new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                                                GridBagConstraints.NORTHWEST,
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(pkiStoreLabel,
                        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                                                GridBagConstraints.NORTHWEST,
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(pkiStore, 
                        new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));

            filePanel.setLayout(new GridBagLayout());
            filePanel.setBorder(BorderFactory.createTitledBorder("Keystore Location"));
            filePanel.add(fileField,
                        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 100, 0));
            filePanel.add(fileButton,
                        new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            add(filePanel, 
                        new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 150, 0));

            trustStorePanel.setLayout(new GridBagLayout());
            trustStorePanel.setBorder(BorderFactory.createTitledBorder("Truststore Location"));
            trustStorePanel.add(trustStoreField,
                        new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 100, 0));
            trustStorePanel.add(trustStoreButton,
                        new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            trustStorePanel.add(trustStorePasswordLabel,
                        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.NONE, 
                                                new Insets(5, 5, 5, 5), 0, 0));
            trustStorePanel.add(trustStorePassword,
                        new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                                                GridBagConstraints.NORTHWEST, 
                                                GridBagConstraints.HORIZONTAL, 
                                                new Insets(5, 5, 5, 5), 100, 0));
            add(trustStorePanel,
                        new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, 
                                                GridBagConstraints.NORTHWEST,
                                                GridBagConstraints.HORIZONTAL,
                                                new Insets(5, 5, 5, 5), 150, 0));

            usePKIBox.addActionListener(this);
            pkiStore.addActionListener(this);
            fileButton.addActionListener(this);
            trustStoreButton.addActionListener(this);

        }

        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == usePKIBox) {
                pkiStore.setEnabled(usePKIBox.isSelected());
                filePanel.setEnabled(usePKIBox.isSelected());
                fileField.setEnabled(usePKIBox.isSelected());
                fileButton.setEnabled(usePKIBox.isSelected());
            }
            else if(e.getSource() == pkiStore) {
                if((pkiStore.getSelectedItem()).equals("PKCS#11")) {
                    filePanel.setBorder(BorderFactory.createTitledBorder("PKCS#11 Library File"));
                    if(ModelUtil.hasLength(localPreferences.getPKCS11Library())) {
                        fileField.setText(localPreferences.getPKCS11Library());
                    } else {
                        fileField.setText("");
                    }
                }
                else if((pkiStore.getSelectedItem()).equals("X.509 PEM File")) {
                    filePanel.setBorder(BorderFactory.createTitledBorder("X.509 Certificate"));
                    //if(ModelUtil.hasLength(localPreferences.getPEMFile())) {
                    //    fileField.setText(localPreferences.getPEMFile());
                    //} else {
                    //    fileField.setText("");
                    //}
                }
                else if((pkiStore.getSelectedItem()).equals("Apple KeyChain")) {
                    filePanel.setBorder(BorderFactory.createTitledBorder("Apple KeyChain"));
                }
                else {
                    filePanel.setBorder(BorderFactory.createTitledBorder("Keystore Location"));
                    if(ModelUtil.hasLength(localPreferences.getJKSPath())) {
                        fileField.setText(localPreferences.getJKSPath());
                    } else {
                        fileField.setText("");
                    }
                }
            }
            else if(e.getSource() == fileButton) {
                int retval = fileChooser.showOpenDialog(this);
                if (retval == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    fileField.setText(file.getAbsolutePath());
                }
            }
            else if(e.getSource() == trustStoreButton) {
                int retval = fileChooser.showOpenDialog(this);
                if (retval == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    trustStoreField.setText(file.getAbsolutePath());
                }
            }
        }

        public boolean validate_settings() {

            boolean valid = true;

            if(usePKIBox.isSelected()) {
                if(!ModelUtil.hasLength(fileField.getText())) {
                    JOptionPane.showMessageDialog(optionsDialog, "You must specify a file location",
                            Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                    fileField.requestFocus();
                    valid = false;
                }
            }
            return valid;
        }

        public void saveSettings() {

            localPreferences.setPKIEnabled(usePKIBox.isSelected());
            localPreferences.setPKIStore((String)pkiStore.getSelectedItem());
            if((pkiStore.getSelectedItem()).equals("PKCS#11")) {
                localPreferences.setPKIStore("PKCS11");
                localPreferences.setPKCS11Library(fileField.getText());
            }
            else if((pkiStore.getSelectedItem()).equals("X.509 Certificate")) {
                localPreferences.setPKIStore("X509");
                //localPreferences.setPEMFile(fileField.getText());
            }
            else if((pkiStore.getSelectedItem()).equals("Apple KeyChain")) {
                localPreferences.setPKIStore("Apple");
            }
            else {
                localPreferences.setPKIStore("JKS");
                localPreferences.setJKSPath(fileField.getText());
            }
            localPreferences.setTrustStorePath(trustStoreField.getText());
            localPreferences.setTrustStorePassword(new String(trustStorePassword.getPassword()));
            SettingsManager.saveSettings();
        }
    }
    
    private class DoubleChannelPanel extends JPanel implements ActionListener{

    	private JLabel title = new JLabel();
    	private JLabel pUsernameLabel = new JLabel();
        private JTextField pUsernameField = new JTextField();
    	private JLabel pPasswordLabel = new JLabel();
        //private JTextField pPasswordField = new JTextField();
    	private JPasswordField pPasswordField = new JPasswordField();
    	private JLabel pServerLabel = new JLabel();
        private JTextField pServerField = new JTextField();
        private JCheckBox enableCheckBox = new JCheckBox();
        
        public DoubleChannelPanel() {
			// TODO Auto-generated constructor stub
        	title.setText("双频道切换配置:");
        	pUsernameLabel.setText("公网用户名:");
        	pPasswordLabel.setText("公网密码:");
        	pServerLabel.setText("公网服务器:");
        	enableCheckBox.setText("启用公网频道:");
        	
        	pUsernameField.setText(localPreferences.getPublicUsername());
        	pPasswordField.setText(localPreferences.getPublicPassword());
        	pServerField.setText(localPreferences.getPublicServer());
        	enableCheckBox.setSelected(localPreferences.isEnablePublic());
        	
        	setLayout(new GridBagLayout());
            add(title, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
            add(pUsernameLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(pUsernameField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
            add(pPasswordLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(pPasswordField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
            add(pServerLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(pServerField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
            add(enableCheckBox, new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

		}
        
        public String getUsername(){
        	return pUsernameField.getText().trim();
        }
        
        public String getPassword(){
        	return new String(pPasswordField.getPassword());
        }
        
        public String getServer(){
        	return pServerField.getText().trim();
        }
        
        public boolean getEnable(){
        	return enableCheckBox.isSelected();
        }
        
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
    	public boolean validate_settings(){
            boolean valid = true;

            if(enableCheckBox.isSelected()) {

            	if(ModelUtil.hasLength(this.getUsername())&&ModelUtil.hasLength(this.getServer())){
            		valid = true;
            	}else{
                    JOptionPane.showMessageDialog(optionsDialog, "需要填写信息",
                            Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
            		valid = false;
            	}
            	
            }
            return valid;
    	}
    	
    	public void saveSettings(){
    		
            localPreferences.setEnablePublic(getEnable());
            
            if (ModelUtil.hasLength(getServer())) {
                localPreferences.setPublicServer(getServer());
            }

            if (ModelUtil.hasLength(getUsername())) {
                localPreferences.setPublicUsername(getUsername());
            }

            if (ModelUtil.hasLength(getPassword())) {
                localPreferences.setPublicPassword(getPassword());
            }
            
            SettingsManager.saveSettings();
    	}
    }
}
